package org.chen.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.chen.common.system.dto.ProvinceCapitalDto;
import org.chen.common.system.province.ProvinceCapital;
import org.chen.system.mapper.ProvinceCapitalMapper;
import org.chen.system.service.ProvinceCapitalService;
import org.springframework.stereotype.Service;

@Service
public class ProvinceCapitalServiceImpl extends ServiceImpl<ProvinceCapitalMapper, ProvinceCapital>
    implements ProvinceCapitalService {
    
    @Override
    public ProvinceCapital findCapitalIdByName(String name) {
        QueryWrapper<ProvinceCapital> wrapper = new QueryWrapper<>();
        wrapper.eq("name", name);
        return this.getOne(wrapper);
    }

    @Override
    public Boolean updateProvinceById(ProvinceCapitalDto capital) {
        ProvinceCapital byId = this.getById(capital.getId());
        if (byId != null && !byId.getIsSystem()) {
            byId.setName(capital.getName());
            byId.setProvince(capital.getProvince());
            byId.setCtripCityId(capital.getCtripId());
            byId.setTongchengCityId(capital.getTongchengId());
            byId.setMafengwoCityId(capital.getMafengwoId());

            this.updateById(byId);

            return true;
        } else {
            throw new RuntimeException("省会城市不存在或无法更新系统默认省会城市");
        }
    }
}